package at.fhj.itm10.ticketmaster.beans;

/**
 * SharingBean.java
 * 
 * @author Marvin Puchmueller
 * 
 */

import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;

import org.apache.log4j.Logger;

import at.fhj.itm10.ticketmaster.dao.DAOException;
import at.fhj.itm10.ticketmaster.dao.impl.DAOFactoryImpl;
import at.fhj.itm10.ticketmaster.entities.Customer;
import at.fhj.itm10.ticketmaster.services.EventService;
import at.fhj.itm10.ticketmaster.services.impl.MailServiceImpl;




public class SharingBean {

	private int eventid;
	private URL url;
	private String username;
	private String eventname;
	private String usergender;
	private String userLastname;
	private String eventdate;
	private String friendsemail = "marvin.puchmueller@hotmail.com";
	private String subject;
	private String content;
	private Customer customer;
	private boolean success;
	private EventService eventservice;
	private DAOFactoryImpl dao = null;
	private MailServiceImpl mailservice;
	
	private static Logger logger= Logger.getLogger("at.fhj.itm10.ticketmaster.bean.SharingBean");
	//Logger logger = Logger.getLogger(SharingBean.class);
	
	
	@PostConstruct
	public void init() {
		dao = new DAOFactoryImpl();
		//eventservice = dao.createEventService();
		mailservice = new MailServiceImpl();
		
	}

	// initializes the Email-Content for mail-receivers
	public String initContent() {
		try {
			setSuccess(true);
			url = new URL("http://localhost:8080/Ticketmaster/event.xhtml?id="
					+ getEventid());
			setContent(getUsergender() + " " + getUserLastname()
					+ " möchte Sie gerne einladen,<br/>" + "an dem Event "
					+ getEventname() + " am " + getEventdate()
					+ " teilzunehmen.<br/>"
					+ "Folgen Sie einfach dem Link:<br/>" + "<a href=\"" + url
					+ "\">http://localhost:8080/Ticketmaster/event.xhtml?id="
					+ getEventid() + "<\\a>");

			return getContent();
		} catch (MalformedURLException e) {
			
			e.printStackTrace();
			setSuccess(false);
			return "";
		}

	}

	// initializes the E-Mail Content for the Sender
	public String initSubjectforUser() {
		setSubject("EventStyria.at - " + getUsername()
				+ " hat Ihnen eine Einladung geschickt!");
		return getSubject();
	}

	// Generates the Content for the User who sends the E-Mail (e.g. changes all
	// "<br/>" to "\n")
	public String initContentforUser() {
		String usercontent;
		initContent();
		usercontent = getContent()
				.replace(
						"<a href=\""
								+ url
								+ "\">http://localhost:8080/Ticketmaster/event.xhtml?id="
								+ getEventid() + "<\\a>",
						"http://localhost:8080/Ticketmaster/event.xhtml?id="
								+ getEventid());
		usercontent = usercontent.replaceAll("<br/>", "\n");
		return usercontent;
	}

	// splits the given mail-adresses at char ";", and removes all spaces
	public void sendMailtoAll(String mailAdress) throws Exception {
		String[] maillist = mailAdress.split(";");
		List<String> mailrecievers = new ArrayList<String>();
		for (int i = 0; i < maillist.length; i++) {
			if (maillist[i].contains(" "))
				maillist[i] = maillist[i].replace(" ", "");		
		}
		
		for (String string : maillist) {
			mailrecievers.add(string);
		}
		mailservice.setContent(getContent());
		mailservice.setSubject(getSubject());
		mailservice.sendMailToAll(mailrecievers);
	}

	/*
	 * kevin ballach email for activation link
	 

	public static void sendActivationMail(String email) throws Exception {
		Session activationMailSession = null;
		
		logger.debug("Method: sendActivationMail");
		logger.info(email);

		try {
			activationMailSession = MailHelper.getSession();
		} catch (NamingException e) {
			e.printStackTrace();
		}

		try {
			MD5HashmakerEmail.generateMD5Code(email);
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}

		try {
			Message message = new MimeMessage(activationMailSession);
			//BodyPart messageBodyPart = new MimeBodyPart();

			// email addresse ändern
			message.setFrom(new InternetAddress(activationMailSession
					.getProperty("mail.from")));
			message.setRecipients(Message.RecipientType.TO,
					InternetAddress.parse(email));
			message.setSubject("Activation");

			URL url = new URL(
					"http://localhost:8080/ticketmaster/activation.xhtml?email="
							+ email + "&hash=" + MD5HashmakerEmail.getMd5code());

			message.setContent(
					"Aktivierungsmail <br/>"
							+ "Bitte klicken Sie den Link um die Aktivierung abzuschließenen! <br/>"
							+ "<br/>"
							+ "<a href=\"" + url + "\">Klick", "text/html");

			Transport.send(message);

			System.out.println("Done");
			System.out.println(email);

			System.out.println(MD5HashmakerEmail.getMd5code());
		} catch (MessagingException e) {
			e.printStackTrace();
			System.err.println("ERROR");
		}
	}*/

	public String getFbEventName(long id) {
		try {
			return eventservice.findById(id).getName();
		} catch (DAOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "";
		}
	}

	public String facebookShareLink(int id) {
		try {
			// TODO url from local host doesn't work. Has to be changed to any
			// public server
			return "http://www.facebook.com/sharer.php?s=100&p[url]=localhost:8080/ticketmaster/event.xhtml?id="
					+ id
					+ "&p[title]="
					+ eventservice.findById((long) id).getName()
					+ "&p[summary]="
					+ eventservice.findById((long) id).getDescription()
					+ "&p[images][0]="
					+ eventservice.findById((long) id).getFlyer();
		} catch (DAOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "";
		} catch (NullPointerException e) {
			// TODO: handle exception
			return "";
		}
	}

	// Getters & Setters

	public String getFriendsemail() {
		return this.friendsemail;
	}

	public void setFriendsemail(String friendsemail) {
		this.friendsemail = friendsemail;
	}

	public String getSubject() {
		return subject;
	}

	public void setSubject(String subject) {
		this.subject = subject;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public URL getUrl() {
		return url;
	}

	public void setUrl(URL url) {
		this.url = url;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getEventname() {
		return eventname;
	}

	public void setEventname(String eventname) {
		this.eventname = eventname;
	}

	public String getUsergender() {
		return usergender;
	}

	public void setUsergender(Boolean usergender) {
		if (usergender == false)
			this.usergender = "Herr";
		else
			this.usergender = "Frau";
	}

	public String getUserLastname() {
		return userLastname;
	}

	public void setUserLastname(String userLastname) {
		this.userLastname = userLastname;
	}

	public String getEventdate() {
		return eventdate;
	}

	public void setEventdate(String eventdate) {
		this.eventdate = eventdate;
	}

	public int getEventid() {
		return eventid;
	}

	public void setEventid(int eventid) {
		this.eventid = eventid;
	}

	public Customer getCustomer() {
		return customer;
	}

	public void setCustomer(Customer customer) {
		this.customer = customer;
	}

	public boolean isSuccess() {
		return success;
	}

	public void setSuccess(boolean success) {
		this.success = success;
	}

}